package chapter3.lc88;

//LC 88 合并两个有序数组

/**
 *
 *  sorted[i + j - 1] = cur
 */
public class MergeSorted {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = 0 , j = 0;
        int[] sorted = new int[m + n];
        int cur = 0;
        while (i < m || j < n) {
            if (i == m) {
                cur = nums2[j];
                j++;
            }else if (j == n) {
                cur = nums1[i];
                i++;
            }else if (nums1[i] > nums2[j]) {
                cur = nums2[j];
                j++;
            }else {
                cur = nums1[i];
                i++;
            }
            sorted[i + j - 1] = cur;
        }
        for (int k = 0; k < sorted.length; k++) {
            nums1[k] = sorted[k];
        }
    }
}
